﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace GeniusAdminWebApplication
{
    public partial class userList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<User> ul = getAllUsers();
            foreach (User u in ul)
            {
                Panel p_item = new Panel();
                p_item.CssClass = "user_item";
                p_item.Controls.Add(new LiteralControl("<span class='user_id'>" + u.id + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='user_name'>" + u.name + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='user_department'>" + u.department.name + "</span>"));

                if (u.status == 0)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='user_status forbin'>冻结</span>"));
                    p_item.Controls.Add(new LiteralControl("<span class='department_action'><input type='button' onclick='recoverUser(" + u.id + ");' value='恢复账号' /></span>"));
                }
                else if (u.status == 1)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='user_status'>正常</span>"));
                    p_item.Controls.Add(new LiteralControl("<span class='department_action'><input type='button' onclick='freezeUser(" + u.id + ");' value='冻结账号' /></span>"));
                }
                else if (u.status == 2)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='user_status'>普通管理员</span>"));
                }
                else if (u.status == 3)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='user_status'>超级管理员</span>"));
                }

                ph_user.Controls.Add(p_item);
            }
        }

        //所有用户
        private IList<User> getAllUsers()
        {
            AdminService adminService = new AdminService();
            return adminService.getAllUser();
        }

        [WebMethod]
        public static string freezeUser(int id)
        {
            AdminService adminService = new AdminService();
            Hashtable ht = adminService.freezeUser(id);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string recoverUser(int id)
        {
            AdminService adminService = new AdminService();
            Hashtable ht = adminService.recoverUser(id);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

    }
}